package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.BookCommentEntity;
import com.jifeng.br.pjj.bookreader.entity.BookEntity;
import com.jifeng.br.pjj.bookreader.mapper.BookCommentMapper;
import com.jifeng.br.pjj.bookreader.mapper.BookMapper;
import com.jifeng.br.pjj.bookreader.service.BookCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 图书评论表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
public class BookCommentServiceImpl extends ServiceImpl<BookCommentMapper, BookCommentEntity> implements BookCommentService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public void saveComment(BookCommentEntity bookComment) {
        bookComment.setCommentTime(new Date());
        this.save(bookComment);
        BookEntity book = new BookEntity();
        book.setBookId(bookComment.getBookId());
        book.setScore(bookComment.getScore());
        bookMapper.updateScore(book);
    }

    @Override
    public IPage<BookCommentEntity> getPage(int page, int limit, BookCommentEntity comment) {
        return this.page(new Page<>(page, limit), new QueryWrapper<BookCommentEntity>()
                .eq(comment.getUserId() != null, "user_id", comment.getUserId())
                .eq(comment.getBookId() != null, "book_id", comment.getBookId())
                .orderByDesc("comment_time")
        );
    }
}
